package com.timothy.millionaire;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
//import android.view.Menu;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.timothy.calculate.CalculatorUtil;

public class Calculate1Activity extends BaseActivity {
	private static final String LOG_TAG = "HomeActivity";
	
	Context mContext;

    TextView yearInvestTV;

	Spinner spinner1;
	ArrayAdapter adapter1;
    String[] invests;
	double yearInvest = 0.0;
    EditText cus_invest;

	Spinner spinner2;
	ArrayAdapter adapter2;
    String[] returns;
	double yearlyReturn = 0.0;
    EditText cus_return;

    TextView result;
    Button calculate;

    TableRow adRow;
    AdView adView;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.calculate1);

        mContext = getApplicationContext();

        yearInvestTV = (TextView) this.findViewById(R.id.yearInvestTV);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        invests = this.getResources().getStringArray(R.array.year_invest_array);
        adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,invests);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(new SpinnerSelectedListener1());
        spinner1.setVisibility(View.VISIBLE);

        cus_invest = (EditText) findViewById(R.id.cus_invest);

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        returns = this.getResources().getStringArray(R.array.year_return_array);
        adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, returns);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);
        spinner2.setOnItemSelectedListener(new SpinnerSelectedListener2());
        spinner2.setVisibility(View.VISIBLE);

        cus_return = (EditText) findViewById(R.id.cus_return);

        result = (TextView) this.findViewById(R.id.result);

        calculate = (Button) this.findViewById(R.id.calculate);
        calculate.setOnClickListener(calculateListener);

//        adView = new AdView(this, AdSize.BANNER, "a151b693eb6195b");
//
//        adRow = (TableRow) this.findViewById(R.id.adRow);
//        adRow.addView(adView);
//
//        AdRequest adRequest = new AdRequest();
//        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
//        adRequest.addTestDevice("SH14CRX00676");

        adView = (AdView) this.findViewById(R.id.adView);
        adView.loadAd(new AdRequest());

	}

	class SpinnerSelectedListener1 implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			Log.i(LOG_TAG,"SpinnerSelectedListener1.onItemSelected()...");
			Log.i(LOG_TAG,"arg2="+String.valueOf(arg2));
            if(arg2 == 10){
                cus_invest.setVisibility(View.VISIBLE);
            }else{
                cus_invest.setVisibility(View.INVISIBLE);
                yearInvest = Double.valueOf(invests[arg2]);
            }
		}
		public void onNothingSelected(AdapterView<?> arg0) {
			Log.i(LOG_TAG,"onNothingSelected()...");
		}
	}

	class SpinnerSelectedListener2 implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			Log.i(LOG_TAG,"SpinnerSelectedListener2.onItemSelected()...");
			Log.i(LOG_TAG,"arg2="+String.valueOf(arg2));
            if(arg2 == 10){
                cus_return.setVisibility(View.VISIBLE);
            }else{
                cus_return.setVisibility(View.INVISIBLE);
                String yearlyReturnStr = returns[arg2].replace("%", "");
                yearlyReturn = 0.01 * Double.valueOf(yearlyReturnStr);
            }
		}
		public void onNothingSelected(AdapterView<?> arg0) {
			Log.i(LOG_TAG,"onNothingSelected()...");
		}
	}

    View.OnClickListener calculateListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (cus_invest.getVisibility() == View.VISIBLE ) {
                if(TextUtils.isEmpty(cus_invest.getText())){
                    ShowDialog("Warning","Year invest must not be empty!");
                    return;
                }

                yearInvest = Double.valueOf(String.valueOf(cus_invest.getText()));
                if(yearInvest < 100000){
                    ShowDialog("Warning","Year Invest must be greater than 100,000");
                    return;
                }
                if(yearInvest > 1000000){
                    ShowDialog("Warning","Year Invest must be less than 100,0000");
                    return;
                }
            }

            if (cus_return.getVisibility() == View.VISIBLE ) {
                if(TextUtils.isEmpty(cus_return.getText())){
                    ShowDialog("Warning","Year return must not be empty!");
                    return;
                }

                yearlyReturn = 0.01 * Double.valueOf(String.valueOf(cus_return.getText()));
                if(yearlyReturn< 0.1 ){
                    ShowDialog("Warning","Year return must be greater than 10%");
                    return;
                }
                if( yearlyReturn > 1){
                    ShowDialog("Warning","Year target must be less than 100%");
                    return;
                }
            }
            int year = CalculatorUtil.getYear(yearInvest, yearlyReturn);
            result.setVisibility(View.VISIBLE);
            result.setText(String.valueOf(year) + " years");
        }
    };


    @Override
    protected void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

/*    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}*/

}
